package com.ct.main;

import com.ct.dao.Employee;
import com.ct.utils.UpdateResult;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Attend {
    public static final String LATE_WORK="迟到";
    public static final String LEAVE_EARLY_WORK="早退";
    public static final String ABSENTEEISM_WORK="旷工";

    public static void main(String[] args) {
        System.out.println(new Date().getDate());
        System.out.println(1.1);
    }
    public static Map<String, Date> attend=new HashMap<>();

    public static void goToWork(Employee employee) {
        System.out.println(employee.toString());
        Date date = new Date();
        Date date1 = attend.get(employee.getId() + "打卡上班" + date.getMonth() + date.getDate());
        if (date1!=null){
            System.out.println("不能重复打上班卡");
            return;
        }
        attend.put(employee.getId()+"打卡上班"+date.getMonth()+date.getDate(),new Date());
    }

    public static void goOffWork(Employee employee){
        JdbcTemplate jdbcTemplate = EmployeePanel.jdbcTemplate;

        //下班打卡时间
        Date on = new Date();
        //上班打卡时间
        Date date1 = attend.get(employee.getId() + "打卡上班" + on.getMonth() + on.getDate());
        if (date1==null){
            try {
                int update = jdbcTemplate.update("insert into attend values (?,?,?,?,?,?)", employee.getId(), employee.getEname(), on, null, ABSENTEEISM_WORK, (1900+ on.getYear()) + "-" + (on.getMonth() + 1) + "-" + on.getDate());
                UpdateResult.isSuccess(update);
            }catch (Exception e){
                System.out.println("打过卡了");
                return;
            }

        }
        Map<String, Object> stringObjectMap =null;
        try{
            stringObjectMap=jdbcTemplate.queryForMap("select * from attend where 'year+month+day' = ? and empid =?", on.getYear() + "+" + on.getMonth() + "+" + on.getDate(),employee.getId());
        }catch (Exception e){

        }
        if (stringObjectMap!=null){
            System.out.println("您已打卡");
            return;
        }

        if (on.getHours()<11&&date1.getHours()>=18){
        //迟到
            try {
                int update = jdbcTemplate.update("insert into attend values (?,?,?,?,?,?)", employee.getId(), employee.getEname(), on, date1, LATE_WORK, (1900+ on.getYear()) + "-" + (on.getMonth() + 1) + "-" + on.getDate());
                UpdateResult.isSuccess(update);
            }catch (Exception e){
                System.out.println("打过卡了");
                return;
            }
        }
        if (on.getHours()>=11||date1.getHours()<16){
        //旷工
            try {
                int update = jdbcTemplate.update("insert into attend values (?,?,?,?,?,?)", employee.getId(), employee.getEname(), on, date1, ABSENTEEISM_WORK, (1900+ on.getYear()) + "-" + (on.getMonth() + 1) + "-" + on.getDate());
                UpdateResult.isSuccess(update);
            }catch (Exception e){
                System.out.println("打过卡了");
                return;
            }
        }
        if (on.getHours()<9&&date1.getHours()<16){
            //早退
            try {
                int update = jdbcTemplate.update("insert into attend values (?,?,?,?,?,?)", employee.getId(), employee.getEname(), on, date1, LEAVE_EARLY_WORK, (1900+ on.getYear()) + "-" + (on.getMonth() + 1) + "-" + on.getDate());
                UpdateResult.isSuccess(update);
            }catch (Exception e){
                System.out.println("打过卡了");
                return;
            }
        }
    }
}
